<?php
namespace Home\Controller;
use Common\Controller\CommonController;

/**
 * 集采员工
 * @author 张亚伟 <zhangyw@zhijin101.com>
 */
class ShopStaffController extends CommonController
{
    
    public function index(){
        $map = [];
        list($list, $page) = $this->lists('ShopStaff', $map, 'id desc');
        if($list){
            // 职位
            $position = yzl_config('shop_glrj.shop_glrj_staff_position');
            // 店铺
            if($shop_ids = array_column($list, 'shop_id')){
                $shop_arr = M('shop')->where(['id' => ['in', $shop_ids]])->getField('id,shopname');
            }else{
                $shop_arr = [];
            }
            // 角色
            $roles = yzl_config('shop_glrj.shop_glrj_staff_role');
            foreach ($list as $key => &$row) {
                if(isset($shop_arr[$row['shop_id']])){
                    $row['shop_name'] = $shop_arr[$row['shop_id']];
                }else{
                    $row['shop_name'] = '';
                }
                // $row['position'] = isset($position[$row['position']]) ? $position[$row['position']] : '';
                $row['role'] = isset($roles[$row['role']]) ? $roles[$row['role']] : '';
            }
        }
        $this->assign('page', $page);
        $this->assign('list', $list);
        $this->display();
    }

    public function edit(){
        $id = I('id');
        if (IS_POST) {
            $data = I('post.');
            $emergency_1 = I('emergency_1');
            $emergency_2 = I('emergency_2');
            $data['emergency_contact'] = $emergency_1.','.$emergency_2;
            $model = D('Api/ShopStaff');

            if($model->create($data)){
                $res = $model->where(['id'=>$id])->save();
                if($res !== false){
                    $this->ajaxReturn(['s'=>true, 'm'=>'修改成功', 'url' => U('index')]);
                }else{
                    $this->ajaxReturn(['s'=>false, 'm'=>'修改失败']);
                }
            }else{
                $this->ajaxReturn(['s'=>false, 'm'=>$model->getError()]);
            }
        } else {
            // 职位
            $position = yzl_config('shop_glrj.shop_glrj_staff_position');
            // 学历
            $education = yzl_config('shop_glrj.shop_glrj_staff_education');
            // 角色
            $roles = yzl_config('shop_glrj.shop_glrj_staff_role');
            $info = M('ShopStaff')->where(['id' => $id])->find();
            $contact = explode(',', $info['emergency_contact']);
            $info['emergency_1'] = isset($contact[0]) ? $contact[0] : '';
            $info['emergency_2'] = isset($contact[1]) ? $contact[1] : '';
            $this->assign('info', $info);
            $this->assign('position', $position);
            $this->assign('education', $education);
            $this->assign('roles', $roles);
            $this->display();
        }
    }

    public function del(){
        $id = I('id');
        $res = M('ShopStaff')->where(['id' => $id])->delete();
        if ($res) {
            $this->ajaxReturn(array('s' => true, 'm' => '删除成功', 'url' => U('index')));
        } else {
            $this->ajaxReturn(array('s' => false, 'm' => '删除失败!'));
        }
    }

    public function wages(){
        $id = I('id');
        $info = M('ShopStaff')->where(['id' => $id])->find();
        if(empty($info)){
            $this->error('记录不存在');
        }
        $wages = $info['wages'] ? json_decode($info['wages'], true) : [];
        $this->assign('wages', $wages);
        $this->assign('id', $id);
        $this->display();
    }

    function wages_edit(){
        $id = I('id');
        $key = I('key');
        $info = M('ShopStaff')->where(['id' => $id])->find();
        if(empty($info)){
            $this->error('记录不存在');
        }
        $wages = $info['wages'] ? json_decode($info['wages'], true) : [];
        if(!isset($wages[$key])){
            $this->error('记录不存在');
        }
        
        if (IS_POST) {
            $data = I('post.');
            $wages[$key] = $data['info'];
            $data = [
                'wages' => json_encode($wages)
            ];
            M('ShopStaff')->where(['id' => $id])->save($data);
            $this->ajaxReturn(array('status' => true, 'm' => '修改成功', 'url' => U('wages', ['id'=>$id, 'key'=>$key])));
        }
        $this->assign('id', $id);
        $this->assign('key', $key);
        $this->assign('info', $wages[$key]);
        $this->display();
    }

    function wages_del(){
        $id = I('id');
        $key = I('key');
        $info = M('ShopStaff')->where(['id' => $id])->find();
        if(empty($info)){
            $this->ajaxReturn(array('s' => false, 'm' => '记录不存在'));
        }
        $wages = $info['wages'] ? json_decode($info['wages'], true) : [];
        if(!isset($wages[$key])){
            $this->ajaxReturn(array('s' => false, 'm' => '记录不存在'));
        }
        unset($wages[$key]);
        $data = [
            'wages' => json_encode($wages)
        ];
        M('ShopStaff')->where(['id' => $id])->save($data);
        $this->ajaxReturn(array('s' => true, 'm' => '删除成功', 'url' => U('index')));
    }
}